//
//  DatabaseManager.m
//  TabBarViewProgrammatically
//
//  Created by Cuong Doan on 1/24/13.
//  Copyright (c) 2013 Cuong Doan. All rights reserved.
//

#import "DatabaseManager.h"

@interface DatabaseManager ()

@end

@implementation DatabaseManager


+(void)openAndPrepareDatabase:(sqlite3 **)db andStatement:(sqlite3_stmt **)statement andSQL:(const char *)sql
{
    NSException *exception;
    
    if(!sqlite3_open([[Common getDatabasePath] UTF8String], db) == SQLITE_OK)
    {
        exception = [NSException exceptionWithName:@"Can't open database" reason:@"Can't open database" userInfo:Nil];
        
        @throw exception;
    }
    
    if(!sqlite3_prepare(*db, sql, -1, statement, NULL) == SQLITE_OK)
    {
        exception = [NSException exceptionWithName:@"Can't prepare database" reason:@"Can't prepare database" userInfo:Nil];
        
        @throw exception;
    }
}

+(void)openAndPrepareDatabaseV2:(sqlite3 **)db andStatement:(sqlite3_stmt **)statement andSQL:(const char *)sql
{
    NSException *exception;
    
    if(!sqlite3_open([[Common getDatabasePath] UTF8String], db) == SQLITE_OK)
    {
        exception = [NSException exceptionWithName:@"Can't open database" reason:@"Can't open database" userInfo:Nil];
        
        @throw exception;
    }
    
    if(!sqlite3_prepare_v2(*db, sql, -1, statement, NULL) == SQLITE_OK)
    {
        exception = [NSException exceptionWithName:@"Can't prepare database" reason:@"Can't prepare database" userInfo:Nil];
        
        @throw exception;
    }
}

+(void)prepareDatabase:(sqlite3 *)db andStatement:(sqlite3_stmt **)statement andSQL:(const char *)sql
{
    NSException *exception;
    
    if(!sqlite3_prepare(db, sql, -1, statement, NULL) == SQLITE_OK)
    {
        exception = [NSException exceptionWithName:@"Can't prepare database" reason:@"Can't prepare database" userInfo:Nil];
        
        @throw exception;
    }
}

+(void)bindText:(sqlite3_stmt *)statement andValue:(NSString *)value andIndex:(int)index
{
    NSException *exception;
    
    if(!sqlite3_bind_text(statement, index, [value UTF8String], -1, SQLITE_TRANSIENT) == SQLITE_OK)
    {
        exception = [NSException exceptionWithName:@"Can't bind text" reason:[NSString stringWithFormat:@"Can't bind text at index: %d", index] userInfo:Nil];
        
        @throw exception;
    }
}

+(void)bindInt:(sqlite3_stmt *)statement andValue:(int)value andIndex:(int)index
{
    NSException *exception;
    
    if(!sqlite3_bind_int(statement, index, value) == SQLITE_OK)
    {
        exception = [NSException exceptionWithName:@"Can't bind int" reason:[NSString stringWithFormat:@"Can't bind int at index: %d", index] userInfo:Nil];
        
        @throw exception;
    }
}

+(void)bindDouble:(sqlite3_stmt *)statement andValue:(double)value andIndex:(int)index
{
    NSException *exception;
    
    if(!sqlite3_bind_double(statement, index, value) == SQLITE_OK)
    {
        exception = [NSException exceptionWithName:@"Can't bind double" reason:[NSString stringWithFormat:@"Can't bind double at index: %d", index] userInfo:Nil];
        
        @throw exception;
    }

}

+(void)bindDate:(sqlite3_stmt *)statement andValue:(NSDate *)date andIndex:(int)index
{
    @try
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        
        [dateFormatter setDateFormat:@"MM/dd/yyyy"];
        
        NSString *dateString = [dateFormatter stringFromDate:date];
        
        [self bindText:statement andValue:dateString andIndex:index];
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }   
}

+(NSDate *)columnDate:(sqlite3_stmt *)statement andIndex:(int)index
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
    
    return [dateFormatter dateFromString:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, index)]];
}
@end
